/**
 * 
 */
package com.module.osr.ui.stormbringer.charviewer.widgets;

import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.opengl.ui.GuiComponent;
import com.dalonedrau.jogl.opengl.ui.Label;
import com.dalonedrau.jogl.opengl.ui.UIFactory;
import com.dalonedrau.jogl.opengl.ui.border.FlexibleImageBorder;
import com.dalonedrau.jogl.opengl.ui.border.StatsBorder;
import com.dalonedrau.jogl.opengl.ui.panel.LandscapePagePanel;
import com.dalonedrau.jogl.opengl.ui.panel.Panel;
import com.dalonedrau.jogl.opengl.ui.panel.PortraitPagePanel;
import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;
import com.module.osr.stormbringer.bus.SBCharacter;
import com.module.stormbringer.globals.Globals;

/**
 * @author Donald
 */
public class PaperDollPanel extends PortraitPagePanel {
	private Label	lblHeight;
	private Label	lblWeight;
	/**
	 * Creates a new instance of {@link PaperDollPanel}.
	 */
	public PaperDollPanel() {
		super(Panel.PORTRAIT_CENTER);
		try {
			super.addChild(PaperDoll.getInstance());
			super.setBorder(new FlexibleImageBorder(
					// bottom-left
					FlexibleImageBorder.BORDER01_BL_LEFT_HALF_BOTTOM_HALF,
					// bottom-right
					FlexibleImageBorder.BORDER05_BR_RIGHT_HALF_BOTTOM_HALF,
					// bottom
					FlexibleImageBorder.BORDER08_B_BOTTOM_HALF,
					// top-left
					FlexibleImageBorder.BORDER11_TL_LEFT_HALF_TOP_HALF,
					// top-right
					FlexibleImageBorder.BORDER15_TR_RIGHT_HALF_TOP_HALF,
					// top
					FlexibleImageBorder.BORDER18_T_TOP_HALF,
					// left
					FlexibleImageBorder.BORDER20_L_LEFT_HALF,
					// right
					FlexibleImageBorder.BORDER22_R_RIGHT_HALF
					));
			super.addChild(getHeightWeightPanel());
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	private Panel getHeightWeightPanel() {
		Panel container = new PortraitPagePanel(Panel.PORTRAIT_LEFT);
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		pnl.setHorizontalAlignment(GuiComponent.TEXT_JUSTIFY);
		Label lbl = UIFactory.getInstance().getStandardH1Label("HT: ");
		lblHeight = getStatsLabel1();
		pnl.addChild(lbl);
		pnl.addChild(lblHeight);
		container.addChild(pnl);

		pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		pnl.setHorizontalAlignment(GuiComponent.TEXT_JUSTIFY);
		lbl = UIFactory.getInstance().getStandardH1Label("WT: ");
		lblWeight = getStatsLabel2();
		pnl.addChild(lbl);
		pnl.addChild(lblWeight);
		container.addChild(pnl);
		return container;
	}
	/**
	 * Gets a "stats" label for display in the viewer.
	 * @return {@link Label}
	 */
	public Label getStatsLabel1() {
		final int w = 51, h = 22;
		Label lbl = UIFactory.getInstance().getStandardH2Label("");
		lbl.setMaximumSize(w, h);
		lbl.setMinimumSize(w, h);
		lbl.setBorder(new StatsBorder());
		return lbl;
	}
	/**
	 * Gets a "stats" label for display in the viewer.
	 * @return {@link Label}
	 */
	public Label getStatsLabel2() {
		final int w = 69, h = 22;
		Label lbl = UIFactory.getInstance().getStandardH2Label("");
		lbl.setMaximumSize(w, h);
		lbl.setMinimumSize(w, h);
		lbl.setBorder(new StatsBorder());
		return lbl;
	}
	/**
	 * Updates the view.
	 * @param pc the {@link SBCharacter} being viewed
	 */
	public void update(final SBCharacter pc) {
		try {
			if (pc.getGender() >= 0
					&& pc.getSkinTone() >= 0) {
				boolean wings = pc.getRace() == Globals.RACE_02MYRRHYN;
				PaperDoll.getInstance().setBody(
						wings,
						new int[] {
								pc.getGender(),
								pc.getSkinTone(),
								pc.getHeight(),
								pc.getBaseBodyFrame(),
								pc.getMuscle(),
								pc.getHairStyle(),
								pc.getHairColor()
				});
			}
			if (pc.getHeightInInches() >= 0) {
				System.out.println("pc.getHeightInInches()::"
						+ pc.getHeightInInches());
				PooledStringBuilder sb =
						StringBuilderPool.getInstance().getStringBuilder();
				int feet = pc.getHeightInInches() / 12;
				int inches = pc.getHeightInInches() % 12;
				System.out.println("feet::" + feet);
				System.out.println("inches::" + inches);
				sb.append(feet);
				sb.append("'");
				if (inches > 0) {
					sb.append(inches);
					sb.append("\"");
				}
				lblHeight.setText(sb.toString());
				sb.setLength(0);
				sb.append(pc.getWeightInPounds());
				sb.append("lbs.");
				lblWeight.setText(sb.toString());
			} else {
				lblHeight.setText("");
				lblWeight.setText("");
			}
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
}
